package com.cskaoyan.service;

import com.cskaoyan.bean.bo.AdminBrandBO;
import com.cskaoyan.bean.bo.AdminBrandDeleteBO;
import com.cskaoyan.bean.bo.AdminBrandUpdateBO;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketBrand;
import com.cskaoyan.bean.po.MarketBrandExample;
import com.cskaoyan.bean.vo.admin_brand.AdminBrandCreateVO;
import com.cskaoyan.bean.vo.admin_brand.AdminBrandListVO;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: Ryan
 * @CreateTime: 2022-09-07  15:01
 */
@Service
public class AdminBrandServiceImpl implements AdminBrandService {
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Override
    public CommonData<AdminBrandListVO> getAdminBrandList(Integer page, Integer limit, String sort, String order, String name, Integer id) {
        // 执行查询之前 → 使用PageHelper开启分页就可以了
        PageHelper.startPage(page, limit);

        MarketBrandExample marketBrandExample = new MarketBrandExample();//增加限制条件
        marketBrandExample.setDistinct(true);
        marketBrandExample.setOrderByClause(sort+" "+order);

        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();

        if (id != null && !"".equals(id)) {
            criteria.andIdEqualTo(id);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%"+name+"%");
        }
        criteria.andDeletedEqualTo(false);

        List<MarketBrand> marketBrandList =marketBrandMapper.selectByExample(marketBrandExample);

        //pageInfo的使用
        PageInfo<MarketBrand> PageInfo = new PageInfo<>(marketBrandList);

        Integer total = Math.toIntExact(PageInfo.getTotal());
        Integer pages = PageInfo.getPages();

        List<AdminBrandListVO> adminBrandList = new LinkedList<>();
        for (MarketBrand marketBrand : marketBrandList) {
            adminBrandList.add(new AdminBrandListVO(marketBrand.getFloorPrice(),marketBrand.getPicUrl(),marketBrand.getDeleted(),marketBrand.getAddTime(),marketBrand.getSortOrder(),marketBrand.getName(),marketBrand.getUpdateTime(),marketBrand.getId(),marketBrand.getDesc()));
        }

        CommonData<AdminBrandListVO> commonData = new CommonData<>(total, pages, limit, page, adminBrandList);
        return commonData;

    }

    @Override
    public AdminBrandCreateVO creatAdminBrand(AdminBrandBO adminBrandBO) {
        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setName(adminBrandBO.getName());
        marketBrand.setDesc(adminBrandBO.getDesc());
        marketBrand.setFloorPrice(adminBrandBO.getFloorPrice());
        marketBrand.setPicUrl(adminBrandBO.getPicUrl());
        marketBrand.setAddTime(new Date(System.currentTimeMillis()));
        marketBrand.setUpdateTime(new Date(System.currentTimeMillis()));

        marketBrandMapper.insertSelective(marketBrand);
        AdminBrandCreateVO adminBrandCreateVO = new AdminBrandCreateVO(marketBrand.getId(),adminBrandBO.getName(),adminBrandBO.getDesc(),adminBrandBO.getPicUrl(),adminBrandBO.getFloorPrice(),marketBrand.getAddTime(),marketBrand.getUpdateTime());
        return adminBrandCreateVO;
    }

    @Override
    public AdminBrandCreateVO updateAdminBrand(AdminBrandUpdateBO adminBrandUpdate) {
        // MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrand marketBrand = new MarketBrand(adminBrandUpdate.getId(),adminBrandUpdate.getName(),adminBrandUpdate.getDesc(),adminBrandUpdate.getPicUrl(),null,adminBrandUpdate.getFloorPrice(),adminBrandUpdate.getAddTime(),new Date(System.currentTimeMillis()),null);
        marketBrandMapper.updateByPrimaryKey(marketBrand);
        AdminBrandCreateVO adminBrandCreateVO = new AdminBrandCreateVO(marketBrand.getId(),marketBrand.getName(),marketBrand.getDesc(),marketBrand.getPicUrl(),marketBrand.getFloorPrice(),marketBrand.getAddTime(),marketBrand.getUpdateTime());

        return adminBrandCreateVO;
    }

    @Override
    public void deleteAdminBrand(AdminBrandDeleteBO adminBrandDeleteBO) {
        marketBrandMapper.deleteByPrimaryKey(adminBrandDeleteBO.getId());
    }
}
